import Cookies from "js-cookie";
import { menusList } from "../../http/api";
import { asyncroute, routes } from "../../router/index";
const state = {
  token: "",
  userInfo: {},
  routers: [],
};
const mutations = {
  token(state, payload) {
    console.log(payload);
    state.token = payload;
    Cookies.set("token", payload);
  },
  // 用户
  userlist(state, data) {
    state.userInfo = data;
  },
  //
  userlogin(state) {
    state.routers = [...routes];
  },
  setuser(state, list) {
    state.routers = [...routes, ...list];
  },
};

const actions = {
  // 登录
  tologins(context, data) {
    console.log(data);
    context.commit("token", data.data);
  },

  async menus({ commit }, txt) {
    let list = null;
    await menusList().then((res) => {
      console.log(res);
      list = res.data;
    });
    console.log(32, list);
    commit("userlist", list);
    return list.roles.menus;
  },

  filteruser({ commit }, txt) {
    console.log(txt);
    return new Promise((resolve, reject) => {
      let men = [];
      txt.forEach((i) => {
        men.push(
          ...asyncroute.filter((item) => {
           return item.path.substr(1) === i;
          })
        );
      });
      commit("setuser", men);
      resolve(men);
    });
  },
};

export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
